<?php namespace App\Http\Controllers\Api;

use App\CoinLog;
use App\Http\Controllers\Controller;
use App\Industry;
use App\Profession;
use App\Signed;
use App\User;
use App\UserAddress;
use App\UserInfo;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function changeUserInfo(Guard $guard,Request $request){
        $sex      = $request->get('sex');
        $nickname = $request->get('nickname');
        $trade    = $request->get('trade');
        $jobName  = $request->get('job_name');
        $birthday = $request->get('birthday');
        $areaInfo = $request->get('area_info');
        $industry = $request->get('industry');
        $industryTwo = $request->get('industry_two');

        $areaInfos = explode(" ",$areaInfo);

        if($guard->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        /** @var User $user */
        $user = $guard->user();

        /** @var UserInfo $userinfo */
        $userinfo = UserInfo::whereUserId($user->id)->first();
        if(!$userinfo)
            return response()->json(['level'=>'error','message'=>'用户信息不存在']);

        if(is_null($userinfo->trade))$userinfo->trade       = $trade;
        if(is_null($userinfo->job))$userinfo->job           = $jobName;
        if(is_null($userinfo->birthday))$userinfo->birthday = $birthday;
        if(is_null($userinfo->province))$userinfo->province = $areaInfos[0];
        if(is_null($userinfo->city))$userinfo->city         = $areaInfos[1];
        if(is_null($userinfo->area))$userinfo->area         = $areaInfos[2];
        if(is_null($userinfo->sex))$userinfo->sex           = $sex;
        if(is_null($userinfo->nickname))$userinfo->nickname = $nickname;
        if(is_null($userinfo->industry))$userinfo->industry = $industry;
        if(is_null($userinfo->industry_two))$userinfo->industry_two = $industryTwo;
        $userinfo->save();

        return response()->json(['level'=>'success','message'=>'设置成功']);
    }

    public function getPro(Request $request){

        /** @var Profession $thatProfession */
        $thatProfession = Profession::wherePid(0)->get();

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$thatProfession]);
    }

    public function getChildPro(Request $request){

        $proName = $request->get('pro_name');

        /** @var Profession $thatProfession */
        $thatProfession = Profession::whereProName($proName)->first();

        if(!$thatProfession)
            return response()->json(["level"=>"error","message"=>"未找到该职业"],203);

        $childProfession = $thatProfession->childPro;

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$childProfession]);
    }

    public function getIndus(Request $request){

        /** @var Industry $thatProfession */
        $thatProfession = Industry::wherePid(0)->get();

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$thatProfession]);
    }

    public function getChildIndus(Request $request){

        $name = $request->get('name');

        /** @var Industry $thatProfession */
        $thatProfession = Industry::whereName($name)->first();

        if(!$thatProfession)
            return response()->json(["level"=>"error","message"=>"未找到该职业"],203);

        $childProfession = $thatProfession->childIndus;

        return response()->json(['level'=>'success','message'=>'获取成功','data'=>$childProfession]);
    }
}